package com.kuang.Dao;

import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;


    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(101, new Employee(101,"!!","124122223.@qwe",1,new Department(101,"aaa")));
        employees.put(102, new Employee(102,"@@","131523123.@qwe",0,new Department(102,"bbb")));
        employees.put(103, new Employee(103,"##","125153523.@qwe",0,new Department(103,"ccc")));
        employees.put(104, new Employee(104,"$$","125163423.@qwe",0,new Department(104,"ddd")));
        employees.put(105, new Employee(105,"%%","123152313.@qwe",1,new Department(105,"eee")));

    }


    private static Integer initId = 106;

    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    public Collection<Employee> getAll() {
        return employees.values();
    }

    public Employee getEmployeeById(Integer initId) {
        return employees.get(initId);
    }

    public void delete(Integer initId) {
        employees.remove(initId);
    }
}
